package com.itheima.health.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.service.OrderService;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisMessageConstant;
import com.itheima.health.utils.sms.SmsUtils;
import com.itheima.health.utils.sms.ValidateCodeUtils;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**

 */
@RestController
@RequestMapping("order")
public class OrderController {

    @Reference
    OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("sendCode/{telephone}")
    public Result sendCode(@PathVariable("telephone") String telephone) {
        String code = ValidateCodeUtils.generateValidateCode(4);
        //调用阿里云发送短信
//        SmsUtils.validUserTelephone(telephone,code);
        System.out.println("验证码:" + code);
        Map map = new HashMap<>();
        map.put("telephone", telephone);
        map.put("code", code);
        rabbitTemplate.convertAndSend("heima-queue",map);
        //使用mq投递手机号和验证码到队列中

        //验证码放入redis中存储90秒,代表该验证码90秒内有效
        RedisUtil.set(RedisMessageConstant.SENDTYPE_ORDER + telephone, code, 90, TimeUnit.SECONDS);
        return new Result(code);
    }

    @PostMapping("add")
    public Result add(@RequestBody Map map) {
        return new Result(orderService.add(map));
    }

    @GetMapping("findOrderInfoByOrderId/{id}")
    public Result findOrderInfoByOrderId(@PathVariable("id") int id) {
        return new Result(orderService.findOrderInfoByOrderId(id));
    }


}
